package org.jeecg.modules.geo.service.resolve;

import org.jeecg.common.util.SpringContextHolder;
import org.jeecg.modules.geo.core.constant.BimGeoEventMouseEventType;
import org.jeecg.modules.geo.core.constant.BimGeoEventOperateType;
import org.jeecg.modules.geo.entity.BimGeoApp;
import org.jeecg.modules.geo.entity.BimGeoEvent;
import org.jeecg.modules.icon.entity.BimGeoIcon;
import org.jeecg.modules.geo.service.IBimGeoAppService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GeoBaseResolve {

    private static final IBimGeoAppService bimGeoAppService = SpringContextHolder.getBean(IBimGeoAppService.class);

    /**
     * 事件拼装
     */
    public static Map<String, Object> resolveEvent(BimGeoEvent bimGeoEvent, String params) {
        BimGeoApp bimGeoApp = bimGeoAppService.getById(bimGeoEvent.getAppId());
        Map<String, Object> actions = new HashMap<>();
        actions.put("ruleType", bimGeoEvent.getRuleType());

        Map<String, Object> act = new HashMap<>();
        act.put("url", bimGeoEvent.getPath());
        act.put("parameter", bimGeoEvent.getParameter().concat(params == null ? "" : params));
        if (BimGeoEventOperateType.EMBED.getValue().equals(bimGeoEvent.getOperateType())) {
            act.put("windowWidth", bimGeoEvent.getWindowWidth());
            act.put("windowHeight", bimGeoEvent.getWindowHeight());
        }
        actions.put(BimGeoEventOperateType.signOf(bimGeoEvent.getOperateType()), act);
        return actions;
    }

    /**
     * 鼠标左键解析
     *
     * @param defaultEvents
     * @param eventsGroupByName
     * @param actions
     * @param params
     * @return
     */
    public static Map<String, Object> resolveLeftEvent(Map<Integer, Map<Integer, List<BimGeoEvent>>> defaultEvents, Map<String, List<BimGeoEvent>> eventsGroupByName, String actions, String params, Integer type) {
        List<BimGeoEvent> leftEvents = null;
        if (actions != null && !actions.isEmpty()) {
            //定制事件
            leftEvents = eventsGroupByName.get(actions);
            if (leftEvents != null) {
                leftEvents = leftEvents.stream().filter(item -> BimGeoEventMouseEventType.LEFT_BUTTON.getValue().equals(item.getMouseEventType())).collect(Collectors.toList());
            }
        }
        if (leftEvents == null || leftEvents.isEmpty()) {
            //默认事件
            Map<Integer, List<BimGeoEvent>> defaults = defaultEvents.get(1);//获取默认event default=1
            if (defaults != null) {
                leftEvents = defaults.get(BimGeoEventMouseEventType.LEFT_BUTTON.getValue());
            }
        }
        if (leftEvents != null) {
            return resolveEvent(leftEvents.get(0), params);
        }
        return null;
    }

    /**
     * 鼠标右键解析
     *
     * @param defaultEvents
     * @param eventsGroupByName
     * @param actions
     * @param params
     * @return
     */
    public static Map<String, Object> resolveRightEvent(Map<Integer, Map<Integer, List<BimGeoEvent>>> defaultEvents, Map<String, List<BimGeoEvent>> eventsGroupByName, String actions, String params, Integer type) {
        List<BimGeoEvent> rightEvents = null;
        if (actions != null && !actions.isEmpty()) {
            //定制事件
            rightEvents = eventsGroupByName.get(actions);
            if (rightEvents != null) {
                rightEvents = rightEvents.stream().filter(item -> BimGeoEventMouseEventType.RIGHT_BUTTON.getValue().equals(item.getMouseEventType())).collect(Collectors.toList());
            }
        }
        if (rightEvents == null || rightEvents.isEmpty()) {
            //默认事件
            Map<Integer, List<BimGeoEvent>> defaults = defaultEvents.get(1);//获取默认event default=1
            if (defaults != null) {
                rightEvents = defaults.get(BimGeoEventMouseEventType.RIGHT_BUTTON.getValue());
            }
        }
        if (rightEvents != null) {
            return resolveEvent(rightEvents.get(0), params);
        }
        return null;
    }

    /**
     * icon/css解析
     *
     * @param defaultIcons
     * @param iconGroupByName
     * @param icon
     * @return
     */
    public static HashMap<String, Object> resolveIcons(Map<Integer, List<BimGeoIcon>> defaultIcons, Map<String, List<BimGeoIcon>> iconGroupByName, String icon, Integer type) {
        List<BimGeoIcon> geoIcons = null;
        if (icon != null && !icon.isEmpty()) {
            //定制
            geoIcons = iconGroupByName.get(icon);
        }
        if (geoIcons == null) {
            //默认
            geoIcons = defaultIcons.get(1); //获取默认icon default=1
        }
//        if (geoIcons != null) {
//            return BimStringUtil.strToMap(geoIcons.get(0).getValue());
//        }
        return null;
    }
}
